<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$this->model('member')->upmember();
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	$_SESSION['checkimg']=1;
	$return['demo']=$this->model('agent')->getdemo();
	if($_RQ['id']){
		$return['post']=pdo_get('jy_aiagent_useragent',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		$return['post']['oldlogo']=$return['post']['logo'];
		$return['post']['logo']=tomedia($return['post']['logo']);
	}else{
		$return['post']=array('ainame'=>'小云','llm_model'=>'qwen','llm_model2'=>0,'desc'=>$return['demo']['desc']);
	}
	$return['post']['goodsid']=intval($_RQ['goodsid']);
	$return['llm_models']=$this->llm_models;
	$return['llm_models2']=array();
	$index=0;
	foreach($return['llm_models'] as $k=>$title){
		if($k==$return['post']['llm_model']){
			$return['post']['llm_model2']=$index;
		}
		$index++;
		$return['llm_models2'][]=array('id'=>$k,'title'=>$title);
	}
	$return['yuyuantypes']=$this->yuyuantypes;
	$return['voices']=array();
	$return['post']['voiceid']=$return['post']['voiceid']?:$return['demo']['voiceid'];
	$return['post']['voicetitle']=pdo_getcolumn('jy_aiagent_voice',array('id'=>$return['post']['voiceid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$return['post']['language']=pdo_getcolumn('jy_aiagent_voice',array('id'=>$return['post']['voiceid'],'uniacid'=>$_SESSION['uniacid']),'language');
	$return['post']['languagetitle']=$this->yuyuantypes[$return['post']['language']];
	foreach($return['pluginset']['logoimgs'] as &$img){
		$img=tomedia($img);
	}
	$return['params']=array('groupid'=>0,'voicesex'=>0,'yuyuantype'=>0);
	$return['sharedata']=array(
		'title'=>$return['basic']['share_title'],
		'desc'=>$return['basic']['share_desc'],
		'link'=>custom_url('jy_aiagent/index//fromuser='.$_SESSION['uid'],true),
		'imgUrl'=>ATTACHMENT_URL.$return['basic']['share_image'],
	);
	set_html('title','编辑智能体');
	exi($return);
}
if($_RQ['op']=='post'){
		$postdata=get_postdata();
		if(!$postdata['title']){
			exi('请填写名称！','error');
		}
		if(!$postdata['voiceid']){
			exi('请选择音色！','error');
		}
		if(!$postdata['llm_model']){
			exi('请选择模型！','error');
		}
		if($_SESSION['wxapp_id'] && $_SESSION['container']=='wxapp'){
			$result=model('core/wxapp_api')->msgSecCheck($postdata['title']);
			if($result['errcode']){
				exi('标题内容可能不合法！','error');
			}
			if($postdata['desc']){
				$result=model('core/wxapp_api')->msgSecCheck($postdata['desc']);
				if($result['errcode']){
					exi('设定描述内容可能不合法！','error');
				}
			}
			if($postdata['ainame']){
				$result=model('core/wxapp_api')->msgSecCheck($postdata['ainame']);
				if($result['errcode']){
					exi('助手名内容可能不合法！','error');
				}
			}
			if($postdata['user_name']){
				$result=model('core/wxapp_api')->msgSecCheck($postdata['user_name']);
				if($result['errcode']){
					exi('主人名内容可能不合法！','error');
				}
			}
			if($postdata['logo']){
				$result=model('core/wxapp_api')->mediaCheckAsync($postdata['logo']);
				if($result['errcode']){
					exi('头像图片可能不合法！','error');
				}
				if($postdata['oldlogo']){
					$imgid=explode('.jpg?',$postdata['logo']);
					if($imgid[1]){
						pdo_update('core_attachment',array('old'=>$postdata['oldlogo']),array('id'=>$imgid[1],'uniacid'=>$_SESSION['uniacid']));
					}
				}
				
			}
		}
		$this->model('agent')->adduseragents($postdata);
		if($postdata['goodsid']){
			exi('提交成功！','','/goodsitem//id='.$postdata['goodsid']);
		}else{
			exi('提交成功！','','/agent//type=1');
		}
		
}